﻿///
// Xecure EXP Sign Control Java Script ver1.1  2001.6.28.
//

var s_ca_ip = "134.100.254.174";
var s_ca_port = 8200;

///////////////////////////////////////////////////////////////////////////////
// 유효한 인증기관 인증서의 CN 을 정확히 적어준다.
//

var s_accept_cert = "finebankCA-TEST";

/////////////////////////////////////////////////////////////////////////////////
// 전자서명, 인증서 갱신, 인증서 폐기시에 인증서 암호오류를 허용회수
// 전자서명 확인창에 보일 메세지

var s_pwd_fail = 3;
var s_sign_desc = "";
var s_envelop_desc = "";

//////////////////////////////////////////////////////////////////////////////////
// 로그인 창에 보일 이미지를 다운로드 받을 URL

var s_bannerUrl =  "http://" + window.location.host + "/XecureExpObject/finebank.bmp";
//var s_bannerPath = window.location.hostname + "_8448_8011";
var s_bannerPath = "image";

///////////////////////////////////////////////////////////////////////////////////
// 인증기관 인증서 다운로드시 인증기관 인증서와 인증서 CN

var s_pCaCertUrl= "http://" + window.location.host + "/XecureExpObject/cacert.bin";
var s_pCaCertName = "finebankCA-TEST";

function process_error2()
{
	var errCode;
	var errMsg;

	if(navigator.appName != 'Netscape') {
        	errCode = document.XESignGB.LastErrCode();
                errMsg = document.XESignGB.LastErrMsg();
        }

	alert( "에러코드 : " + errCode + "\n\n" + errMsg );
}

function escape_url(url) {
	var i;
	var ch;
	var out = '';
	var url_string = '';

	url_string = String(url);

	for (i = 0; i < url_string.length; i++) {
		ch = url_string.charAt(i);
		if (ch == ' ')
		    out += '%20';
		else if (ch == '%')
		    out += '%25';
		else if (ch == '&')
		    out += '%26';
		else if (ch == '+')
		    out += '%2B';
		else if (ch == '=')
		    out += '%3D';
		else if (ch == '?')
		    out += '%3F';
		else
		    out += ch;
	}
	return out;
}

function RandomGenerator( num )
{
	var rand;

	if(navigator.appName != 'Netscape')
		rand = document.XESignGB.RandomGen( num );

	return rand;
}

// option : 0 : 서명 원문 출력 안함
//	    1 : 서명 원문 출력
// mask : 0 : all certificates
//        1 : only user certificates
//        2 : only coperation certificates

function Sign_with_option2( option, plain )
{
        var signed_msg;

        if(navigator.appName != 'Netscape')
              signed_msg = document.XESignGB.SignData( s_accept_cert, plain, option, s_sign_desc, s_bannerPath, s_pwd_fail );

        if( signed_msg == "" )
                process_error2();

       	return signed_msg;
}

function VerifySignedData (signed_data)
{
        var ori_msg;
        var msg;
        var agent;
        var position;
        var signer_dn = "";
        var msg1;
        var cert;

        agent = UserAgent();

    	if(navigator.appName != 'Netscape')
        	ori_msg = document.XESignGB.VerifySignedData(signed_data);

        if( ori_msg == "" ) {
            	process_error2();
	}
	else {
		position = ori_msg.indexOf(";");

		if( position == -1 ) {
			msg1 = ori_msg;
			signer_dn = "";
		}
		else {
			msg1 = ori_msg.substring(0, position-1);
			signer_dn = ori_msg.substring(position+1, ori_msg.length );
		}

		msg = '서명 확인 결과 \n\n[ 원문 메세지 ] ';
		msg += "\n\n";
		msg += msg1;

		if( signer_dn != "" ) {
			msg += "\n\n[서명자 DN]\n\n";
			msg += signer_dn;
			cert = document.XESignGB.GetSignerCert();
		}
		else {
			msg += "\n\n[서명자 검증 오류]\n\n";
        		if(navigator.appName != 'Netscape') {
        			errCode = document.XESignGB.LastErrCode();
        			errMsg = document.XESignGB.LastErrMsg();
			}

			msg += "오류코드 : ";
			msg += errCode;
			msg += "\n오류 메세지 : ";
			msg += errMsg;
		}
	}
	return ori_msg;
}

function GetSignerCert() {
	var	signerCert;

	if(navigator.appName != 'Netscape')
		signerCert = document.XESignGB.GetSignerCert();

	return signerCert;
}

// option : 0 : 전자봉투 원문 출력 안함
//	    1 : 전자봉투 원문 출력
function Envelop_with_option (pem, data, option)
{
        var envData;

        if(navigator.appName != 'Netscape')
                envData = document.XESignGB.EnvelopData( pem, data, option, s_envelop_desc);

	if ( envData == '' )
                process_error2();

        return envData;
}

function UnsealData( envelopedData )
{
        var data;

       	if(navigator.appName != 'Netscape')
               	data = document.XESignGB.UnsealData( envelopedData, s_bannerPath, s_accept_cert, 3);

        if ( data == '' ) {
                process_error2();
		return data;
	}

        return data;
}

function trim(str)
{
	var i;
	var ch;
	var out = '';
	var url_string = '';

	string = String(str);

	for (i = 0; i < string.length; i++)
	{
		ch = string.charAt(i);
		if (ch != ' ')
		    out += ch ;
	}

	return out ;
}

function RequestCertificate ( ref_code, auth_code, class_id )
{
	var r;

	if(navigator.appName != 'Netscape')

		r = document.XESignGB.RequestCertificate ( trim(ref_code), trim(auth_code), class_id, s_ca_ip, s_ca_port );

	if ( r != 0 )

                process_error2();

	return r;
}

function RequestCertificateEx ( ref_code, auth_code, class_id, storage )
{
	var r;

	if(navigator.appName != 'Netscape')
		r = document.XESignGB.RequestCertificateEx ( ref_code, auth_code, class_id, s_ca_ip, s_ca_port, storage );

	if ( r != 0 )
		process_error2();

	return r;
}


function RenewCertificate ( )
{
	var r;

	if(navigator.appName != 'Netscape')
		r = document.XESignGB.RenewCertificate ( s_ca_ip, s_ca_port, s_bannerPath, s_accept_cert, s_pwd_fail );

	if ( r != 0 )
                process_error2();

	return r;
}

function RevokeCertificate ( reason )
{
	var r;

	if(navigator.appName != 'Netscape')
		r = document.XESignGB.RevokeCertificate ( s_ca_port, s_ca_ip, reason, s_bannerPath, s_accept_cert, s_pwd_fail );

	if ( r != 0 && r != -1)
                process_error2();

	return r;
}

function RevokeCertificateEx ( reason, storage )
{
	var r;

	if(navigator.appName != 'Netscape')

		r = document.XESignGB.RevokeCertificateEx ( s_ca_port, s_ca_ip, reason, s_bannerPath, s_accept_cert, s_pwd_fail, storage );

	if ( r != 0 && r != -1)
		process_error2();

	return r;
}


function PutBannerUrl()
{
	if(navigator.appName != 'Netscape')
		document.XESignGB.PutBannerUrl( s_bannerPath, s_bannerUrl);
}

function PutCACert()
{
	var r;
	//alert("PutCACert(" + s_pCaCertName + "," + s_pCaCertUrl + ") Call!!!") ;
/*
	if(navigator.appName != 'Netscape')
		r = document.XESignGB.PutCACert( s_pCaCertName, s_pCaCertUrl);

	if( r != 0 ) 	process_error2();
*/
}

function CertManager()
{
	if(navigator.appName != 'Netscape')
		document.XESignGB.CertManager();
}

function UserAgent()
{
	var str;
	var agent;

	str = navigator.userAgent;
	agent = str.substring(0,9);
	return agent;
}

function PrintObjectTag()
{
	var tag;

        if(navigator.appName == 'Netscape') {
		alert('Netscape is Not Support!');
	}
	else {
		tag = '<OBJECT ID="XESignGB" CLASSID="CLSID:7974F5A3-7C2B-4E3B-A033-2C62FF0F4938"></OBJECT>';
	}
	document.write(tag);
}

function PrintDownLoadObjectTag()
{
	var tag;

        if(navigator.appName == 'Netscape') {
		alert('Netscape is Not Support!');
	}
	else {
		tag = '<OBJECT ID="XESignGB" CLASSID="CLSID:7974F5A3-7C2B-4E3B-A033-2C62FF0F4938" CODEBASE="/cab/XESignGB_Install.cab#Version=2,1,5,0"></OBJECT>';
	}
	document.write(tag);
}

function PrintXecureSSOObjectTag()
{
	var tag;

    if(navigator.appName == 'Netscape') {
		alert('Netscape is Not Support!');
	}
	else {
		//tag = '<OBJECT ID="XecureSSO" width=0 heigh=0 CLASSID="CLSID:5F2C54-C8B3-4A93-854B-B2431D3"></OBJECT>';
		tag = '<OBJECT ID="XecureSSO" width=0 heigh=0 CLASSID="CLSID:5F2BEC54-C8B3-4A93-854B-B24B44AD31D3"></OBJECT>';
	}
	document.write(tag);
}

function PrintSecuridSSOObjectTag()
{
	var tag;

    if(navigator.appName == 'Netscape') {
		alert('Netscape is Not Support!');
	}
	else {
		tag = '<OBJECT ID="SecuridSSO" CLASSID="CLSID:63A3E70D-FA8A-4A38-92D1-76EB9682F4F5"></OBJECT>';
	}
	document.write(tag);
}

